package com.ce.pms.charge.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.ICharStoChoGeneralDao;
import com.ce.pms.charge.dao.IChargeItemDao;
import com.ce.pms.charge.dao.IChargeStaDao;
import com.ce.pms.charge.dao.IFixedOrOtherVersionDao;
import com.ce.pms.charge.dao.IGeneralVersionDao;
import com.ce.pms.charge.dao.IMeterVersionDao;
import com.ce.pms.charge.model.AbstractStaVersion;
import com.ce.pms.charge.model.ChargeItemDomain;
import com.ce.pms.charge.model.ChargeStaDomain;
import com.ce.pms.charge.model.GeneralVersion;
import com.ce.pms.charge.model.MeterVersion;
import com.ce.pms.charge.model.OtherVersion;
import com.ce.pms.charge.model.SectSta;
import com.ce.pms.charge.service.ICharStoChoFixedService;
import com.ce.pms.charge.service.ICharStoChoGeneralService;
import com.ce.pms.charge.service.IChargeStaService;
import com.ce.pms.charge.vo.ChargeStaVO;
import com.ce.pms.charge.vo.ChargeStaVersionVO;
import com.ce.pms.charge.vo.GeneralVersionVO;
import com.ce.pms.charge.vo.MeterVersionVO;
import com.ce.pms.charge.vo.SectionStandardVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/**
 * 功能描述: 收费标准版本功能处理类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-8-13
 * Edit author:
 * Edit on:
 * 修改原因：
 */
public class ChargeStaServiceImpl extends PmsBaseServiceImpl implements
		IChargeStaService {
	/**
	 * 方法描述: 根据Id查询收费标准VO
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param chargeStaId
	 * @return ChargeStaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ChargeStaVO findChargeStaById(Long chargeStaId){
		super.checkParameterIsNull(chargeStaId, "common:parameters_null_exception,ID");
		ChargeStaDomain chargeStaDomain = chargeStaDao.findById(chargeStaId);
		ChargeStaVO chargeStaVO = this.convertChargeStaD2V(chargeStaDomain);
		//检查收费标准是否被引用，设置相应状态,主要为页面修改操作提供支持
//		if(checkChargeSta(chargeStaId)){
//			chargeStaVO.setReferSelect(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
//		}
		return chargeStaVO;
	}
	/**
	 * 方法描述: 根据收费标准Id查询当前收费标准版本VO
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param chargeStaId
	 * @return ChargeStaVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ChargeStaVersionVO findVersionByChargeStaId(Long chargeStaId){
		//验证必填项
		super.checkParameterIsNull(chargeStaId, "common:parameters_null_exception,ID");

		ChargeStaDomain chargeStaDomain = chargeStaDao.findById(chargeStaId);
		//所属的收费标准为常规，则返回常规版本VO
		if(chargeStaDomain.getChargeItem().getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL)){
			if(!BlankUtil.isBlank(chargeStaDomain.getCVersion())){
				return this.findGeneralVersionById(chargeStaDomain.getCVersion().getStaHisId());
			}return null;
		}
		//所属的收费标准为走表，则返回走表VO
		if(chargeStaDomain.getChargeItem().getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER)){

			if(!BlankUtil.isBlank(chargeStaDomain.getCVersion())){
				return this.findMeterVersionById(chargeStaDomain.getCVersion().getStaHisId());
			}return null;
		}
		//所属的收费标准为固定或其他，则返回固定或其他版本VO
		if(chargeStaDomain.getChargeItem().getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED)||chargeStaDomain.getChargeItem().getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_OTHER)){

			if(!BlankUtil.isBlank(chargeStaDomain.getCVersion())){
				return this.findFixedOrOtherVersionById(chargeStaDomain.getCVersion().getStaHisId());
			}return null;
		}
		return null;
	}
	/**
	 * 方法描述: 根据Id查询常规收费标准历史版本VO
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param generalVersionId
	 * @return GeneralVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private GeneralVersionVO findGeneralVersionById(Long generalVersionId){
		super.checkParameterIsNull(generalVersionId, "common:parameters_null_exception,ID");
		GeneralVersion generalVersion = generalVersionDao.findById(generalVersionId);
		return this.convertGeneralVersionD2V(generalVersion);
	}
	/**
	 * 方法描述: 根据Id查询走表收费标准历史版本VO
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param meterVersionId
	 * @return MeterVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private MeterVersionVO findMeterVersionById(Long meterVersionId){
		super.checkParameterIsNull(meterVersionId, "common:parameters_null_exception,ID");
		MeterVersion meterVersion = meterVersionDao.findById(meterVersionId);
		return this.convertMeterVersionD2V(meterVersion);
	}
	/**
	 * 方法描述: 根据Id查询固定收费标准历史版本VO或其他收费标准历史版本VO
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param otherVersionId
	 * @return ChargeStaVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private ChargeStaVersionVO findFixedOrOtherVersionById(Long chargeStaVersionId){
		super.checkParameterIsNull(chargeStaVersionId, "common:parameters_null_exception,ID");
		AbstractStaVersion abstractStaVersion = fixedOrOtherVersionDao.findById(chargeStaVersionId);
		return this.convertStaVersionD2V(abstractStaVersion);
	}
	/**
	 * 方法描述: 分页查询某个收费项目下的收费标准列表
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param chargeItemId,pageNo, pageSize
	 * @return Page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryChargeSatByPage(Long chargeItemId, int pageNo, int pageSize) {
		super.checkParameterIsNull(chargeItemId, "common:parameters_null_exception,ID");
		Page page = chargeStaDao.queryChargeSatByPage(chargeItemId, pageNo,
				pageSize);
		List<ChargeStaDomain> chargeStaDomainList = new ArrayList<ChargeStaDomain>();
		List<ChargeStaVO> chargeStaVOList = new ArrayList<ChargeStaVO>();
		chargeStaDomainList = page.getResult();
		for (ChargeStaDomain chargeStaDomain : chargeStaDomainList) {
			ChargeStaVO chargeStaVO = this.convertChargeStaD2V(chargeStaDomain);
			chargeStaVOList.add(chargeStaVO);
		}
		page.setResult(chargeStaVOList);
		return page;
	}

	/**
	 * 方法描述: 查询某个收费项目下所有收费标准
	 *
	 * @author 胡煦 Create on:2008-8-18
	 * @param chargeItemId
	 * @return List<ChargeStaVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<ChargeStaVO> queryAllChargeSatBy(Long chargeItemId) {
		List<ChargeStaDomain> chargeStaDomainList = chargeStaDao.queryAllChargeSatBy(chargeItemId);
		List<ChargeStaVO> chargeStaVOList = new ArrayList<ChargeStaVO>();
		for (ChargeStaDomain chargeStaDomain : chargeStaDomainList) {
			ChargeStaVO chargeStaVO = this.convertChargeStaD2V(chargeStaDomain);
			chargeStaVOList.add(chargeStaVO);
		}
		return chargeStaVOList;
	}

	/**
	 * 方法描述:收费标准及版本新增方法
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param ChargeStaVO,ChargeStaVersionVO,sectionStandardVOSet
	 * @return ChargeStaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ChargeStaVO addChargeStaAndVersion(ChargeStaVO chargeStaVO,ChargeStaVersionVO staVersionVO,Set<SectionStandardVO> sectionStandardVOSet){

		chargeStaVO = this.addChargeSta(chargeStaVO);
		ChargeItemDomain chargeItemDomain = chargeItemDao.findById(chargeStaVO.getChargeItemId());
		//绑定收费版本与收费标准的关系
		staVersionVO.setChargeStaId(chargeStaVO.getKeyword());
		//所属的收费标准为常规，则调用常规版本新增
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL)){
			this.addGeneralVersionSta((GeneralVersionVO) staVersionVO, sectionStandardVOSet);
		}
		//所属的收费标准为走表，则调用走表版本新增
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER)){
			this.addMeterVersion((MeterVersionVO)staVersionVO, sectionStandardVOSet);
		}
		//所属的收费标准为固定或其他，则调用固定或其他版本新增
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED)||chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_OTHER)){
			this.addFixedOrOtherVersion(staVersionVO);
		}
		return chargeStaVO;
	}
	/**
	 * 方法描述:收费标准及版本修改方法
	 * @author 胡煦
	 * Create on:2008-8-8
	 * @param ChargeStaVO,ChargeStaVersionVO,sectionStandardVOSet
	 * @return ChargeStaVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ChargeStaVO editChargeStaAndVersion(ChargeStaVO chargeStaVO,ChargeStaVersionVO staVersionVO,Set<SectionStandardVO> sectionStandardVOSet){

		chargeStaVO = this.editChargeSta(chargeStaVO);
		ChargeItemDomain chargeItemDomain = chargeItemDao.findById(chargeStaVO.getChargeItemId());
		//绑定收费版本与收费标准的关系
		staVersionVO.setChargeStaId(chargeStaVO.getKeyword());
		//获取当前版本ID
		staVersionVO.setStaHisId(chargeStaVO.getCurrentVersionId());
		//所属的收费标准为常规，则调用常规版本修改
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL)){
			GeneralVersion generalVersion = this.editGeneralVersionSta((GeneralVersionVO) staVersionVO, sectionStandardVOSet);
			//更新指定收费标准的房间标准选用表中的费用
			charStoChoGeneralService.updateCharstChoAmt(chargeStaVO.getKeyword(),generalVersion);
		}
		//所属的收费标准为走表，则调用走表版本修改
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER)){
			this.editMeterVersion((MeterVersionVO)staVersionVO, sectionStandardVOSet);
		}
		//所属的收费标准为固定或其他，则调用固定或其他版本修改
		if(chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED)||chargeItemDomain.getChrgType().equals(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_OTHER)){
			OtherVersion otherVersion = this.editFixedOrOtherVersion(staVersionVO);
			//更新指定收费标准的房间标准选用表中的费用
			charStoChoFixedService.updateCharstChoAmt(chargeStaVO.getKeyword(),otherVersion);
		}
		return chargeStaVO;
	}
	/**
	 * 方法描述:新增收费标准
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVO
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private ChargeStaVO addChargeSta(ChargeStaVO chargeStaVO) {
		if (checkChargeStaNameIsOnly(chargeStaVO.getStaName(), null)) {
			throw new BusinessException("charge:chargeItem_name_exist_exception",
					BusinessException.WARNING);
		}
		ChargeStaDomain chargeStaDomain = chargeStaDao.saveObject(this
				.convertChargeStaV2D(chargeStaVO));
		chargeStaVO.setKeyword(chargeStaDomain.getKeyword());
		return chargeStaVO;
	}

	/**
	 * 方法描述:修改收费标准
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVO
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private ChargeStaVO editChargeSta(ChargeStaVO chargeStaVO) {
		checkChargeStaVONotNullField(chargeStaVO,
				"common:mustFill_null_exception");
		if (checkChargeStaNameIsOnly(chargeStaVO.getStaName(), chargeStaVO
				.getKeyword())) {
			throw new BusinessException(
					"charge:chargeSta_name_exist_exception",
					BusinessException.WARNING);
		}
		ChargeStaDomain chargeStaDomain = chargeStaDao.saveOrUpdateObject(this
				.convertChargeStaV2D(chargeStaVO));
		return chargeStaVO;
	}

	/**
	 * 方法描述:删除收费标准
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param chargeItemId
	 * @return void Edit author: Edit on: 修改原因：
	 */
	public void deleteChargeSta(Long chargeStaId) {
		super.checkParameterIsNull(chargeStaId,
				"common:parameters_null_exception,ID");
		if(checkChargeSta(chargeStaId)){
			throw new BusinessException("common:delete_error_exception",
					BusinessException.WARNING);
		}
		try {
			chargeStaDao.deleteObjectById(chargeStaId);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 方法描述: 根据收费标准名称查询记录数
	 * 注：新建判断chargeStaName的唯一性时，chargeStaId为null；修改判断chargeStaName的唯一性时，则需要传入修改前的chargeStaId<br>
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param chargeStaName
	 * @return boolean：返回true表示已经被引用，false则未被引用
	 * Edit author: Edit on: 修改原因：
	 */
	public boolean checkChargeStaNameIsOnly(String chargeStaName,
			Long chargeStaId) {
		super.checkParameterIsNull(chargeStaName,
				"common:parameters_null_exception,seqNo");
		return chargeStaDao
				.checkChargeStaNameIsOnly(chargeStaName, chargeStaId) != 0;
	}
	/**
	 * 方法描述:新增常规收费标准历史版本
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVersionVO
	 * @return ChargeStaVersionVO Edit author: Edit on: 修改原因：
	 */
	private GeneralVersionVO addGeneralVersionSta(
			GeneralVersionVO generalVersionVO,Set<SectionStandardVO> sectionStandardVOSet) {


		GeneralVersion generalVersion = this.convertGeneralVersionV2D(generalVersionVO);
		//将状态设置为当前有效
		generalVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		//新增修改分段标准,通过级联实现
		generalVersion.setSecStas(this.convertStandardSetV2D(generalVersion, sectionStandardVOSet));

		generalVersion = generalVersionDao.saveObject(generalVersion);

		generalVersionVO.setStaHisId(generalVersion.getStaHisId());

		return generalVersionVO;
	}

	/**
	 * 方法描述:修改常规收费标准历史版本
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVO
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private GeneralVersion editGeneralVersionSta(
			GeneralVersionVO generalVersionVO,Set<SectionStandardVO> sectionStandardVOSet) {

		checkGeneralVersionVONotNullField(generalVersionVO,
		"common:mustFill_null_exception");

		GeneralVersion generalVersion = generalVersionDao.findById(generalVersionVO.getStaHisId());
		//收费标准版本更新为不可用状态
		generalVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_OVERDUE);
		modifyInfo(generalVersion.getSysInfo(), generalVersionVO.getModifier());
		//删除ID以使操作变为新增
		generalVersionVO.setStaHisId(null);

		GeneralVersion version = this.convertGeneralVersionV2D(generalVersionVO);
		//新增修改分段标准,通过级联实现
		version.setSecStas(this.convertStandardSetV2D(version, sectionStandardVOSet));
		//将状态设置为当前有效
		version.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		generalVersionDao.saveOrUpdateObject(version);

		return version;
	}

	/**
	 * 方法描述:新增走表收费标准历史版本
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVO
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private MeterVersionVO addMeterVersion(MeterVersionVO meterVersionVO,Set<SectionStandardVO> sectionStandardVOSet) {

		MeterVersion meterVersion = this.convertMeterVersionV2D(meterVersionVO);

		meterVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		//新增分段标准,通过级联增加
		meterVersion.setSecStas(this.convertStandardSetV2D(meterVersion, sectionStandardVOSet));
		meterVersion = meterVersionDao.saveObject(meterVersion);

		meterVersionVO.setStaHisId(meterVersion.getStaHisId());
		return meterVersionVO;
	}

	/**
	 * 方法描述:修改走表收费标准历史版本
	 *
	 * @author 胡煦 Create on:2008-8-6
	 * @param ChargeStaVO
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private MeterVersion editMeterVersion(MeterVersionVO meterVersionVO,Set<SectionStandardVO> sectionStandardVOSet) {
		checkMeterVersionVONotNullField(meterVersionVO,
		"common:mustFill_null_exception");
		//找到修改前的收费标准版本
		MeterVersion meterVersion = meterVersionDao.findById(meterVersionVO.getStaHisId());
		//收费标准版本更新为不可用状态
		meterVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_OVERDUE);
		modifyInfo(meterVersion.getSysInfo(), meterVersionVO.getModifier());
		//删除ID以使操作变为新增
		meterVersionVO.setStaHisId(null);
		//新增分段标准,通过级联增加
		MeterVersion version = this.convertMeterVersionV2D(meterVersionVO);
		version.setSecStas(this.convertStandardSetV2D(version, sectionStandardVOSet));
		version.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		meterVersionDao.saveOrUpdateObject(version);

		return version;
	}

	/**
	 * 方法描述:新增固定或其他收费标准历史版本
	 * @author 胡煦
	 * Create on:2008-8-6
	 * @param ChargeStaVersionVO
	 * @return ChargeStaVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private ChargeStaVersionVO addFixedOrOtherVersion(ChargeStaVersionVO chargeStaVersionVO){

		AbstractStaVersion staVersion = this.convertOtherVersionV2D(chargeStaVersionVO);
		staVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		staVersion = (OtherVersion) fixedOrOtherVersionDao.saveObject(staVersion);

		chargeStaVersionVO.setStaHisId(staVersion.getStaHisId());
		return chargeStaVersionVO;
	}
	/**
	 * 方法描述:修改固定或其他收费标准历史版本
	 * @author 胡煦
	 * Create on:2008-8-6
	 * @param ChargeStaVersionVO
	 * @return ChargeStaVersionVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private OtherVersion editFixedOrOtherVersion(ChargeStaVersionVO chargeStaVersionVO){
		//找到修改前的收费标准版本
		AbstractStaVersion staVersion = fixedOrOtherVersionDao.findById(chargeStaVersionVO.getStaHisId());
		//收费标准版本更新为不可用状态
		staVersion.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_OVERDUE);
		modifyInfo(staVersion.getSysInfo(), chargeStaVersionVO.getModifier());
		//删除ID以使操作变为新增
		chargeStaVersionVO.setStaHisId(null);
		OtherVersion version = this.convertOtherVersionV2D(chargeStaVersionVO);
		version.setStatuz(SysrefcodeDO.FEE_STA_VERSION_STATUZ_VALID);
		fixedOrOtherVersionDao.saveOrUpdateObject(version);

		return version;
	}
	/**
	 * 方法描述: ChargeStaDomain to ChargeStaVO
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param chargeStaDomain
	 * @return ChargeStaVO Edit author: Edit on: 修改原因：
	 */
	private ChargeStaVO convertChargeStaD2V(ChargeStaDomain chargeStaDomain) {
		ChargeStaVO chargeStaVO = new ChargeStaVO();
		if (BlankUtil.isBlank(chargeStaDomain)) {
			return chargeStaVO;
		}
		PopulateUtil.populate(chargeStaDomain, chargeStaVO);
		chargeStaVO.setChargeItemName(chargeStaDomain.getChargeItem() == null ? null
				: chargeStaDomain.getChargeItem().getItemName());
		chargeStaVO
				.setChargeItemId(chargeStaDomain.getChargeItem() == null ? null
						: chargeStaDomain.getChargeItem().getKeyword());
		chargeStaVO.setCurrentVersionId(chargeStaDomain.getCVersion() == null ? null
				: chargeStaDomain.getCVersion().getStaHisId());
		Set<Long> staHisIdSet = new HashSet<Long>();
		if(!BlankUtil.isBlank(chargeStaDomain.getHisVersions())){
		for (AbstractStaVersion staVersion : chargeStaDomain.getHisVersions()) {
			Long id = staVersion.getStaHisId();
			staHisIdSet.add(id);
		}
		chargeStaVO.setStaHisId(staHisIdSet);
		}
		return chargeStaVO;
	}



	/**
	 * 方法描述: chargeStaVO to chargeStaDomain
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param chargeStaVO
	 * @return ChargeStaDomain Edit author: Edit on: 修改原因：
	 */
	private ChargeStaDomain convertChargeStaV2D(ChargeStaVO chargeStaVO) {
		ChargeStaDomain chargeStaDomain = new ChargeStaDomain();
		if (BlankUtil.isBlank(chargeStaVO)) {
			return chargeStaDomain;
		}
		PopulateUtil.populate(chargeStaVO, chargeStaDomain);
		chargeStaDomain.setChargeItem(chargeItemDao.findById(chargeStaVO
				.getChargeItemId()));
		if(!BlankUtil.isBlank(chargeStaVO
				.getCurrentVersionId())){
			chargeStaDomain.setCVersion(fixedOrOtherVersionDao.findById(chargeStaVO
				.getCurrentVersionId()));
		}
		Set<AbstractStaVersion> staVersionSet = new HashSet<AbstractStaVersion>();
		if(!BlankUtil.isBlank(chargeStaVO.getStaHisId())){
		for (Long staHisId : chargeStaVO.getStaHisId()) {
			AbstractStaVersion staVersion = fixedOrOtherVersionDao.findById(staHisId);
			staVersionSet.add(staVersion);
		}
		chargeStaDomain.setHisVersions(staVersionSet);
		}
		return chargeStaDomain;
	}
	/**
	 * 方法描述: GeneralVersionVO to GeneralVersion
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param GeneralVersionVO
	 * @return GeneralVersion Edit author: Edit on: 修改原因：
	 */
	private GeneralVersion convertGeneralVersionV2D(
			GeneralVersionVO generalVersionVO) {
		GeneralVersion generalVersion = new GeneralVersion();
		if (BlankUtil.isBlank(generalVersionVO)) {
			return generalVersion;
		}
		PopulateUtil.populate(generalVersionVO, generalVersion);

		if (!BlankUtil.isBlank(generalVersionVO.getChargeStaId())){
			generalVersion.setChargeSta(chargeStaDao.findById(generalVersionVO
				.getChargeStaId()));
		}

		if(!BlankUtil.isBlank(generalVersionVO.getSectStaVOSet())){
			Set<SectSta> sectStaSet = new HashSet<SectSta>();
			for (SectionStandardVO standardVO : generalVersionVO.getSectStaVOSet()) {
				sectStaSet.add(this.convertSectStaV2D(standardVO));
			}
			generalVersion.setSecStas(sectStaSet);
		}

		if (!BlankUtil.isBlank(generalVersionVO.getCreator())) {
			SysInfo sysInfo = createInfo(generalVersionVO.getCreator());
			generalVersion.setSysInfo(sysInfo);
		}


		return generalVersion;
	}

	/**
	 * 方法描述: GeneralVersion to GeneralVersionVO
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param GeneralVersion
	 * @return GeneralVersionVO Edit author: Edit on: 修改原因：
	 */
	private GeneralVersionVO convertGeneralVersionD2V(
			GeneralVersion generalVersion) {
		GeneralVersionVO generalVersionVO = new GeneralVersionVO();
		if (BlankUtil.isBlank(generalVersion)) {
			return generalVersionVO;
		}
		PopulateUtil.populate(generalVersion, generalVersionVO);
		generalVersionVO
				.setChargeStaId(generalVersion.getChargeSta() == null ? null
						: generalVersion.getChargeSta().getKeyword());
		Set<SectionStandardVO> sectStaVOSet = new TreeSet<SectionStandardVO>();
		//处理分段收费标准
		if(!BlankUtil.isBlank(generalVersion.getSecStas())){
			for (SectSta sectSta : generalVersion.getSecStas()) {
				sectStaVOSet.add(this.convertSectStaD2V(sectSta));
			}
			generalVersionVO.setSectStaVOSet(sectStaVOSet);
		}
		//设置页面显示需要的是否收取滞纳金
//		if(generalVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
//			generalVersionVO.setLatefeeSelectStr("否");
//		}
//		if(generalVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE)){
//			generalVersionVO.setLatefeeSelectStr("是");
//		}
		//设置页面显示需要的计费参数
		generalVersionVO.setCountModeDisplay(sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.FEE_STA_VERSION_COUNT_MODE, generalVersionVO.getCountMode()).getName());
		//设置页面显示需要的计费方式
		generalVersionVO.setCountParameterDisplay(sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.FEE_STA_VERSION_COUNT_CODE_PARAMETER, generalVersionVO.getCountParameter()).getName());
		generalVersionVO.setCreator(generalVersion.getSysInfo() == null ? null
				: generalVersion.getSysInfo().getCreator());
		generalVersionVO.setSysDate(generalVersion.getSysInfo() == null ? null
				: generalVersion.getSysInfo().getSysDate());
		generalVersionVO.setModifier(generalVersion.getSysInfo() == null ? null
				: generalVersion.getSysInfo().getModifier());
		generalVersionVO
				.setModifyDate(generalVersion.getSysInfo() == null ? null
						: generalVersion.getSysInfo().getModifyDate());
		return generalVersionVO;
	}
	/**
	 * 方法描述: SectSta to SectionStandardVO
	 * @author 胡煦
	 * Create on:2008-8-7
	 * @param SectSta
	 * @return SectionStandardVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public SectionStandardVO convertSectStaD2V(SectSta sectSta){

		SectionStandardVO sectionStandardVO = new SectionStandardVO();
		PopulateUtil.populate(sectSta, sectionStandardVO);
		sectionStandardVO.setCreator(sectSta.getSysInfo() == null ? null
				: sectSta.getSysInfo().getCreator());
		sectionStandardVO.setModifier(sectSta.getSysInfo() == null ? null
				: sectSta.getSysInfo().getModifier());
		return sectionStandardVO;
	}
	/**
	 * 方法描述: sectionStandardVO to SectSta
	 * @author 胡煦
	 * Create on:2008-8-7
	 * @param sectionStandardVO
	 * @return SectSta
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public SectSta convertSectStaV2D(SectionStandardVO sectionStandardVO){

		SectSta sectSta = new SectSta();
		PopulateUtil.populate(sectionStandardVO, sectSta);

		if (!BlankUtil.isBlank(sectionStandardVO.getCreator())) {
			SysInfo sysInfo = createInfo(sectionStandardVO.getCreator());
			sectSta.setSysInfo(sysInfo);
		}

		return sectSta;
	}

	/**
	 * 功能描述:根据标准的名称，查询固定收费项目的有有效收费标准及价格<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<ChargeStaVO> queryChargeSelectList(String chargeStaName){
		List<ChargeStaVO> list=new ArrayList();
		List<Object[]> listojb=chargeStaDao.queryChargeSelectList(chargeStaName);
		if(listojb.size()>0){
			for(Object[] ojb: listojb){
				ChargeStaVO vo = new ChargeStaVO();
				vo.setStaName(ojb[0].toString());
				vo.setPrice(new BigDecimal(ojb[1].toString()));
				list.add(vo);
			}
		}
		return list;
	}

	/**
	 * 方法描述: MeterVersionVO to meterVersion
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param MeterVersionVO
	 * @return meterVersion Edit author: Edit on: 修改原因：
	 */
	private MeterVersion convertMeterVersionV2D(
			MeterVersionVO meterVersionVO) {
		MeterVersion meterVersion = new MeterVersion();
		if (BlankUtil.isBlank(meterVersionVO)) {
			return meterVersion;
		}
		PopulateUtil.populate(meterVersionVO, meterVersion);

		meterVersion.setLQuantitySelect(meterVersionVO.getQuantitySelect());
		meterVersion.setLQuantity(meterVersionVO.getQuantity());
		if (!BlankUtil.isBlank(meterVersionVO.getChargeStaId())){
			meterVersion.setChargeSta(chargeStaDao.findById(meterVersionVO
					.getChargeStaId()));
		}

		if(!BlankUtil.isBlank(meterVersionVO.getSectStaVOSet())){
			Set<SectSta> sectStaSet = new HashSet<SectSta>();
			for (SectionStandardVO standardVO : meterVersionVO.getSectStaVOSet()) {
				sectStaSet.add(this.convertSectStaV2D(standardVO));
			}
			meterVersion.setSecStas(sectStaSet);
		}

		if (!BlankUtil.isBlank(meterVersionVO.getCreator())) {
			SysInfo sysInfo = createInfo(meterVersionVO.getCreator());
			meterVersion.setSysInfo(sysInfo);
		}

		return meterVersion;
	}

	/**
	 * 方法描述: meterVersion to MeterVersionVO
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param MeterVersionVO
	 * @return MeterVersionVO Edit author: Edit on: 修改原因：
	 */
	private MeterVersionVO convertMeterVersionD2V(
			MeterVersion meterVersion) {
		MeterVersionVO meterVersionVO = new MeterVersionVO();
		if (BlankUtil.isBlank(meterVersion)) {
			return meterVersionVO;
		}
		PopulateUtil.populate(meterVersion, meterVersionVO);
		meterVersionVO.setQuantity(meterVersion.getLQuantity());
		meterVersionVO.setQuantitySelect(meterVersion.getLQuantitySelect());
		//设置页面显示需要的计费方式
		meterVersionVO.setCountModeDisplay(sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.FEE_STA_VERSION_COUNT_MODE, meterVersion.getCountMode()).getName());
		//设置页面显示需要的用量类型
		meterVersionVO.setDosageTypeDisplay(sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.FEE_STA_VERSION_DOSAGE_TYPE, meterVersion.getDosageType()).getName());
//		if(meterVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
//			meterVersionVO.setLatefeeSelectStr("否");
//		}
//		if(meterVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE)){
//			meterVersionVO.setLatefeeSelectStr("是");
//		}
//		if(meterVersion.getLQuantitySelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
//			meterVersionVO.setQuantitySelectStr("否");
//		}
//		if(meterVersion.getLQuantitySelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE)){
//			meterVersionVO.setQuantitySelectStr("是");
//		}

		meterVersionVO
				.setChargeStaId(meterVersion.getChargeSta() == null ? null
						: meterVersion.getChargeSta().getKeyword());

		Set<SectionStandardVO> sectStaVOSet = new HashSet<SectionStandardVO>();
		if(!BlankUtil.isBlank(meterVersion.getSecStas())){
		for (SectSta sectSta : meterVersion.getSecStas()) {
			sectStaVOSet.add(this.convertSectStaD2V(sectSta));
		}
		meterVersionVO.setSectStaVOSet(sectStaVOSet);
		}
		meterVersionVO.setCreator(meterVersion.getSysInfo() == null ? null
				: meterVersion.getSysInfo().getCreator());
		meterVersionVO.setSysDate(meterVersion.getSysInfo() == null ? null
				: meterVersion.getSysInfo().getSysDate());
		meterVersionVO.setModifier(meterVersion.getSysInfo() == null ? null
				: meterVersion.getSysInfo().getModifier());
		meterVersionVO
				.setModifyDate(meterVersion.getSysInfo() == null ? null
						: meterVersion.getSysInfo().getModifyDate());
		return meterVersionVO;
	}


	/**
	 * 方法描述: ChargeStaVersionVO to OtherVersion
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param chargeStaVersionVO
	 * @return OtherVersion Edit author: Edit on: 修改原因：
	 */
	private OtherVersion convertOtherVersionV2D(
			ChargeStaVersionVO chargeStaVersionVO) {
		OtherVersion otherVersion = new OtherVersion();
		if (BlankUtil.isBlank(chargeStaVersionVO)) {
			return otherVersion;
		}
		PopulateUtil.populate(chargeStaVersionVO, otherVersion);
		if(!BlankUtil.isBlank(chargeStaVersionVO
				.getChargeStaId())){
		otherVersion.setChargeSta(chargeStaDao.findById(chargeStaVersionVO
				.getChargeStaId()));
		}
		if (!BlankUtil.isBlank(chargeStaVersionVO.getCreator())) {
			SysInfo sysInfo = createInfo(chargeStaVersionVO.getCreator());
			otherVersion.setSysInfo(sysInfo);
		}


		return otherVersion;
	}

	/**
	 * 方法描述: AbstractStaVersion to ChargeStaVersionVO
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param AbstractStaVersion
	 * @return ChargeStaVersionVO Edit author: Edit on: 修改原因：
	 */
	private ChargeStaVersionVO convertStaVersionD2V(
			AbstractStaVersion staVersion) {
		ChargeStaVersionVO chargeStaVersionVO = new ChargeStaVersionVO();
		if (BlankUtil.isBlank(staVersion)) {
			return chargeStaVersionVO;
		}
		PopulateUtil.populate(staVersion, chargeStaVersionVO);
//		if(staVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
//			chargeStaVersionVO.setLatefeeSelectStr("否");
//		}
//		if(staVersion.getLatefeeSelect().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE)){
//			chargeStaVersionVO.setLatefeeSelectStr("是");
//		}
		chargeStaVersionVO
				.setChargeStaId(staVersion.getChargeSta() == null ? null
						: staVersion.getChargeSta().getKeyword());

		chargeStaVersionVO.setCreator(staVersion.getSysInfo() == null ? null
				: staVersion.getSysInfo().getCreator());
		chargeStaVersionVO.setSysDate(staVersion.getSysInfo() == null ? null
				: staVersion.getSysInfo().getSysDate());
		chargeStaVersionVO.setModifier(staVersion.getSysInfo() == null ? null
				: staVersion.getSysInfo().getModifier());
		chargeStaVersionVO
				.setModifyDate(staVersion.getSysInfo() == null ? null
						: staVersion.getSysInfo().getModifyDate());
		return chargeStaVersionVO;
	}
//	/**
//	 * 方法描述: ChargeStaVersionVO to FixedVersion
//	 *
//	 * @author 胡煦 Create on:2008-8-1
//	 * @param chargeStaVersionVO
//	 * @return FixedVersion Edit author: Edit on: 修改原因：
//	 */
//	private FixedVersion convertFixedVersionV2D(
//			ChargeStaVersionVO chargeStaVersionVO) {
//		FixedVersion fixedVersion = new FixedVersion();
//		if (BlankUtil.isBlank(chargeStaVersionVO)) {
//			return fixedVersion;
//		}
//		PopulateUtil.populate(chargeStaVersionVO, fixedVersion);
//		if(!BlankUtil.isBlank(chargeStaVersionVO
//				.getChargeStaId())){
//		fixedVersion.setChargeSta(chargeStaDao.findById(chargeStaVersionVO
//				.getChargeStaId()));
//		}
//		if (!BlankUtil.isBlank(chargeStaVersionVO.getCreator())) {
//			SysInfo sysInfo = createInfo(chargeStaVersionVO.getCreator());
//			fixedVersion.setSysInfo(sysInfo);
//		}
//		return fixedVersion;
//	}
//

	/**
	 * 方法描述: Set<SectionStandardVO> to Set<SectSta>
	 *
	 * @author 胡煦 Create on:2008-8-1
	 * @param Set<SectionStandardVO>,AbstractStaVersion
	 * @return Set<SectSta> Edit author: Edit on: 修改原因：
	 */
	private Set<SectSta> convertStandardSetV2D(AbstractStaVersion staVersion, Set<SectionStandardVO> sectionStandardVOSet){
		Set<SectSta> sectStaSet = new HashSet<SectSta>();
		if(!BlankUtil.isBlank(sectionStandardVOSet)){
			for(SectionStandardVO sectionStandardVO : sectionStandardVOSet){
				SectSta sectSta = this.convertSectStaV2D(sectionStandardVO);
				sectSta.setStaHis(staVersion);
				sectStaSet.add(sectSta);
			}
		}
		return sectStaSet;
	}

	/**
	 * 方法描述: 检查标准收费标准版本VO中必填项是否为空
	 *
	 * @author 胡煦 Create on:2008-7-30
	 * @param chargeItemVO,key
	 * @return void Edit author: Edit on: 修改原因：
	 */
	private void checkGeneralVersionVONotNullField(
			GeneralVersionVO generalVersionVO, String key) {
		if (generalVersionVO.getCountParameter() == null
				|| generalVersionVO.getCountMode() == null
				|| generalVersionVO.getLatefeeSelect() == null) {
			throw new BusinessException(key, BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: 检查收费标准是否被引用
	 *
	 * @author 胡煦 Create on:2008-7-30
	 * @param chargeStaId
	 * @return boolean ：true表示被引用，false表示没有
	 * Edit author: Edit on: 修改原因：
	 */
	private boolean checkChargeSta(Long chargeStaId) {
		return charStoChoGeneralDao.checkExistCharStoChoByStaId(chargeStaId);
	}

	/**
	 * 方法描述: 检查收费标准VO中必填项是否为空
	 *
	 * @author 胡煦 Create on:2008-7-30
	 * @param chargeItemVO,key
	 * @return void Edit author: Edit on: 修改原因：
	 */
	private void checkChargeStaVONotNullField(ChargeStaVO chargeStaVO,
			String key) {
		if (chargeStaVO.getStaName() == null
				|| chargeStaVO.getChargeItemId() == null
				|| chargeStaVO.getCurrentVersionId() == null) {
			throw new BusinessException(key, BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 检查走表收费标准版本VO中必填项是否为空
	 *
	 * @author 胡煦 Create on:2008-7-30
	 * @param chargeItemVO,key
	 * @return void Edit author: Edit on: 修改原因：
	 */
	private void checkMeterVersionVONotNullField(
			MeterVersionVO meterVersionVO, String key) {
		if (meterVersionVO.getDosageType() == null
				|| meterVersionVO.getCountMode() == null
				|| meterVersionVO.getLatefeeSelect() == null
				||meterVersionVO.getQuantitySelect() == null
				||meterVersionVO.getDynameter() == null){
			throw new BusinessException(key, BusinessException.WARNING);
		}
	}
	private IChargeItemDao chargeItemDao;

	private IChargeStaDao chargeStaDao;


	private IGeneralVersionDao generalVersionDao;

	private IMeterVersionDao meterVersionDao;

	private IFixedOrOtherVersionDao fixedOrOtherVersionDao;

	private ICharStoChoGeneralDao charStoChoGeneralDao;

	private ICharStoChoFixedService charStoChoFixedService;

	private ICharStoChoGeneralService charStoChoGeneralService;

	/**
	 * @param charStoChoGeneralDao the charStoChoGeneralDao to set
	 */
	public void setCharStoChoGeneralDao(ICharStoChoGeneralDao charStoChoGeneralDao) {
		this.charStoChoGeneralDao = charStoChoGeneralDao;
	}
	/**
	 * @param meterVersionDao the meterVersionDao to set
	 */
	public void setMeterVersionDao(IMeterVersionDao meterVersionDao) {
		this.meterVersionDao = meterVersionDao;
	}

	/**
	 * @param chargeStaDao
	 *            the chargeStaDao to set
	 */
	public void setChargeStaDao(IChargeStaDao chargeStaDao) {
		this.chargeStaDao = chargeStaDao;
	}

	/**
	 * @param generalVersionDao
	 *            the generalVersionDao to set
	 */
	public void setGeneralVersionDao(IGeneralVersionDao generalVersionDao) {
		this.generalVersionDao = generalVersionDao;
	}

	/**
	 * @param chargeItemDao the chargeItemDao to set
	 */
	public void setChargeItemDao(IChargeItemDao chargeItemDao) {
		this.chargeItemDao = chargeItemDao;
	}
	/**
	 * @param fixedOrOtherVersionDao the fixedOrOtherVersionDao to set
	 */
	public void setFixedOrOtherVersionDao(
			IFixedOrOtherVersionDao fixedOrOtherVersionDao) {
		this.fixedOrOtherVersionDao = fixedOrOtherVersionDao;
	}
	/**
	 * @param charStoChoFixedService the charStoChoFixedService to set
	 */
	public void setCharStoChoFixedService(
			ICharStoChoFixedService charStoChoFixedService) {
		this.charStoChoFixedService = charStoChoFixedService;
	}
	/**
	 * @param charStoChoGeneralService the charStoChoGeneralService to set
	 */
	public void setCharStoChoGeneralService(
			ICharStoChoGeneralService charStoChoGeneralService) {
		this.charStoChoGeneralService = charStoChoGeneralService;
	}


}
